import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入对应的页面
// 账号管理
import AccountAdd from "@/views/account/AccountAdd.vue"
import AccoutList from "@/views/account/AccoutList.vue"
import ModifyPassword from "@/views/account/ModifyPassword.vue"
import PersonalCenter from "@/views/account/PersonalCenter.vue"

// 商品管理
import GoodsAdd from "@/views/goods/GoodsAdd.vue"
import GoodsCategroy from "@/views/goods/GoodsCategroy.vue"
import GoodsList from "@/views/goods/GoodsList.vue"

import { Message } from 'element-ui';

// 后台首页
import Home from "@/views/home/Home.vue"

// 后台大页面，分为左右两部分
import Layout from "@/views/layout/Layout.vue"

// 登录页面
import Login from "@/views/login/Login.vue"

// 订单管理
import OrderManage from "@/views/order/OrderManage.vue"
import OrderMore from "@/views/order/OrderMore.vue"

// 销售统计
import GoodsTotal from "@/views/sell/GoodsTotal.vue"
import OrderTotal from "@/views/sell/OrderTotal.vue"

// 店铺管理
import ShopManage from "@/views/shop/ShopManage.vue"

Vue.use(VueRouter)

const routes = [

  // 配置一级路由
  { path: "/", redirect: "/Layout" },
  // 登录路由
  { path: "/login", component: Login },
  // 后台大页面
  { path: "/layout", component: Layout },
  // 下面是二级路由
  // 账号管理
  {
    path: "/Account",
    component: Layout,
    redirect: "/Account/AccoutList",
    meta: { title: "账号管理" },
    children: [
      { path: "/Account/AccoutList", meta: { title: "账号列表" }, component: AccoutList },
      { path: "/Account/AccountAdd", meta: { title: "账号添加" }, component: AccountAdd },
      { path: "/Account/ModifyPassword", meta: { title: "修改密码" }, component: ModifyPassword },
      { path: "/Account/PersonalCenter", meta: { title: "个人中心" }, component: PersonalCenter },
    ]
  },

  // 商品管理
  {
    path: "/Goods",
    component: Layout,
    redirect: "/Goods/GoodsAdd",
    meta: { title: "商品管理" },
    children: [
      { path: "/Goods/GoodsAdd", meta: { title: "商品添加" }, component: GoodsAdd },
      { path: "/Goods/GoodsCategroy", meta: { title: "商品分类" }, component: GoodsCategroy },
      { path: "/Goods/GoodsList", meta: { title: "商品列表" }, component: GoodsList }
    ]
  },

  // 订单管理
  {
    path: "/Order",
    component: Layout,
    redirect: "/Order/OrderManage",
    meta: { title: "订单管理" },
    children: [
      { path: "/Order/OrderManage", component: OrderManage },
      { path: "/Order/OrderMore", component: OrderMore },
    ]
  },

  // 后台首页
  {
    path: "/Home",
    component: Layout,
    redirect: "/Home/Home",
    meta: { title: "后台首页" },
    children: [
      { path: "/Home/Home", component: Home }
    ]
  },

  // 销售统计
  {
    path: "/Total",
    component: Layout,
    redirect: "/Total/GoodsTotal",
    meta: { title: "销售统计" },
    children: [
      { path: "/Total/GoodsTotal", meta: { title: "商品统计" }, component: GoodsTotal },
      { path: "/Total/OrderTotal", meta: { title: "订单统计" }, component: OrderTotal },
    ]
  },

  // 店铺管理
  {
    path: "/Shop",
    component: Layout,
    redirect: "/Shop/ShopManage",
    meta: { title: "店铺管理" },
    children: [
      { path: "/Shop/ShopManage", component: ShopManage }
    ]
  },
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  // 如果跳转的页面是登录页，那么就不要求它有token，让它直接跳转
  if (to.path == "/login") {
    next()
  } else {
    let token = localStorage.getItem("token")
    if (token) {
      next()
    } else {
      // this.$router.push("/login")
      Message({
        type: "error",
        message: "登录失效，请重新登录，2秒之后跳转到登录界面！"
      })
      setTimeout(() => {
        next("/login")
      }, 2000);
    }
  }
})

export default router
